package com.yxc.plan.wolf.controller;

import com.yxc.plan.wolf.exception.HttpStatusRuntimeException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Description :
 * @Author : abc@vanke.com
 * @Date : 2018/12/11 11:31 AM
 */

@RestController
@RequestMapping("/api/v1")
public class UploadFileController {

    @Value("${file.upload.path}")
    private String filePath;

    @PostMapping(value = "/upload")
    public String imageUpload(MultipartFile file, HttpServletRequest request) {
        try {
            String fileName = file.getOriginalFilename();
            File targetFile = new File(filePath);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            File destFile = new File(filePath + fileName);
            file.transferTo(destFile);
            return destFile.getAbsolutePath();
//            return "/opt/paas/data/share/upload/demo_rabbitmq-0.0.1-SNAPSHOT.jar";
        } catch (Exception e) {
            throw new HttpStatusRuntimeException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), null);
        }
    }


}
